package com.twu.biblioteca;

import java.io.PrintStream;
import java.util.Scanner;

public enum Menu {
    ListBooks("1", "1.List Books") {
        @Override
        public ViewState execute(PrintStream ps, Scanner scanner, BookCollection bookCollection, MovieCollection movieCollection, UserCollection userCollection, User currentUser) {
            ps.println(bookCollection.toFormattedString());
            return new ViewState(currentUser,true);
        }
    },
    CheckoutBook("2", "2.Checkout book") {
        @Override
        public ViewState execute(PrintStream ps, Scanner scanner, BookCollection bookCollection, MovieCollection movieCollection, UserCollection userCollection, User currentUser) {
            ps.println(bookCollection.toFormattedString());
            if (currentUser==null) {
                currentUser = login(ps, scanner, userCollection);
            }
            if (currentUser != null) {
                ps.println("Choose book to be checked out");
                String bookChosen = scanner.next();
                if (bookCollection.checkOutItem(bookChosen)) {
                    ps.println("Thank you!Enjoy the book!");
                } else {
                    ps.println("That book is not available.");
                }
            }else {
                ps.println("Invalid User");
            }
            return new ViewState(currentUser,true);
        }
    },
    ReturnBook("3", "3.Return book") {
        @Override
        public ViewState execute(PrintStream ps, Scanner scanner, BookCollection bookCollection, MovieCollection movieCollection, UserCollection userCollection, User currentUser) {
            if (currentUser==null) {
                currentUser = login(ps, scanner, userCollection);
            }
            if (currentUser != null) {
                ps.println("Choose book to be returning");
                String bookForReturn = scanner.next();
                if (bookCollection.returnItem(bookForReturn)) {
                    ps.println("Thank you for returning the book");
                } else {
                    ps.println("That is not a valid book to return.");
                }
            }else {
                ps.println("Invalid User");
            }
            return new ViewState(currentUser,true);
           }

    },
    ListMovie("4", "4.List Movie") {
        @Override
        public ViewState execute(PrintStream ps, Scanner scanner, BookCollection bookCollection, MovieCollection movieCollection, UserCollection userCollection, User currentUser) {
            ps.println(movieCollection.toFormattedString());
            return new ViewState(currentUser,true);
        }


    },
    CheckoutMovie("5", "5.Checkout Movie") {
        @Override
        public ViewState execute(PrintStream ps, Scanner scanner, BookCollection bookCollection, MovieCollection movieCollection, UserCollection userCollection, User currentUser) {
            ps.println(movieCollection.toFormattedString());
            ps.println("Choose movie to be checked out");
            String movieChosen = scanner.next();
            if (movieCollection.checkOutItem(movieChosen)) {
                ps.println("Thank you!Enjoy the Movie!");
            } else {
                ps.println("That Movie is not available.");
            }
            return new ViewState(currentUser,true);

        }
    },
    Quit("6", "6.Quit") {
        @Override
        public ViewState execute(PrintStream ps, Scanner scanner, BookCollection bookCollection, MovieCollection movieCollection, UserCollection userCollection, User currentUser) {
            ps.println("You have chosen to quit! Thank you for using Application!");
            return new ViewState(currentUser,false);
        }
    },
    CustomerInformation("7", "7.Display Customer Information") {
        @Override
        public ViewState execute(PrintStream ps, Scanner scanner, BookCollection bookCollection, MovieCollection movieCollection, UserCollection userCollection, User currentUser) {
            ps.println(currentUser);
            return new ViewState(currentUser,true);
        }
    },
    InvalidOption("8", "Invalid Option") {
        @Override
        public ViewState execute(PrintStream ps, Scanner scanner, BookCollection bookCollection, MovieCollection movieCollection, UserCollection userCollection, User currentUser) {
            ps.println("Invalid Option Menu! Kindly choose again!");
            return new ViewState(currentUser,true);
        }
    };

    public String id;
    private String name;

    Menu(String id, String s) {
        name = s;
        this.id = id;
    }

    public abstract ViewState execute(PrintStream ps, Scanner scanner,
                                      BookCollection bookCollection, MovieCollection movieCollection, UserCollection userCollection, User currentUser);

    @Override
    public String toString() {
        return this.name;
    }

    public User login(PrintStream ps,Scanner scanner,UserCollection userCollection) {
            ps.println("Enter your Library id");
            String id = scanner.next();
            ps.println("Enter your Password");
            String password = scanner.next();
            return userCollection.checkUser(id, password);
        }


}
